package com.thread.concurrentTest;

import org.junit.Test;

import java.util.concurrent.*;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class ThreadTest {

    @Test
    public void test1() {

        Thread thread = new Thread(() -> {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName());
        });

        thread.start();
        System.out.println("=========================");
        thread.start();
    }

    @Test
    public void test2() {
        ReentrantLock lock = new ReentrantLock();
        Condition condition = lock.newCondition();
        new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + "拿到lock前");
            try {
                lock.lock();
//                condition.await();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
//                lock.unlock();
            }
            System.out.println(Thread.currentThread().getName() + "拿到lock后");
        }).start();
        new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + "拿到lock前");
            try {
                lock.lock();
//                condition.await();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
//                lock.unlock();
            }
            System.out.println(Thread.currentThread().getName() + "拿到lock后");
        }).start();
    }

    @Test
    public void test3() {
        CountDownLatch countDownLatch = new CountDownLatch(10);
        for (int i = 0; i < 10; ++i) {
            new Thread(() -> {
                System.out.println(Thread.currentThread().getName() + " countDown前");
                countDownLatch.countDown();
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + " countDown后");
            }).start();
        }
        new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + " await前");
            try {
                countDownLatch.await();
                System.out.println(Thread.currentThread().getName() + " await后等待5s");
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + " await后");
        }, "main-thread").start();

        try {
            Thread.sleep(60000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test4() {
        CountDownLatch countDownLatch = new CountDownLatch(10);
        new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + " await前");
            try {
                countDownLatch.await();
                System.out.println(Thread.currentThread().getName() + " await后等待5s");
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + " await后");
        }, "main-thread").start();
        for (int i = 0; i < 10; ++i) {
            countDownLatch.countDown();
        }

        try {
            Thread.sleep(60000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test5() {
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        executorService.execute(() -> {
            System.out.println(Thread.currentThread().getName());
        });
    }

}
